package cn.edu.nuc.crm.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.nuc.crm.dao.AccountMapper;
import cn.edu.nuc.crm.dto.PageInfo;
import cn.edu.nuc.crm.entity.Account;
import cn.edu.nuc.crm.service.interfaces.AccountService;
@Service
public class AccountServiceImpl implements AccountService {
	@Autowired
	private AccountMapper accountMapper;
	@Override
	public Account login(String accountname, String accountpassword) {
		
		Account account = accountMapper.findByAccount(accountname);
		System.out.println(account);
		if( account == null){
			throw new RuntimeException("用户名错误");
		}else if( !accountpassword.equals(account.getAccountpassword())){
			throw new RuntimeException("密码错误");
		}
		return account;
	}

	/**
	 * 判断用户是否存在
	 * @param acccountname
	 * @param accountpassword
	 * @return
	 */
	public int register(Account account) {
		Account acc = accountMapper.findByAccount(account.getAccountname());
		/*System.out.println(acc);*/
		if( acc == null){
			int count = accountMapper.insert(account);
			return count;
		}else{
			throw new RuntimeException("用户名已存在");
		}	
	}	
	
	
	
	
	public List<Account> account(){
		List<Account> account=new ArrayList<Account>();
		account =accountMapper.account();
		return account;	
	}
	public PageInfo account(int current) {
		PageInfo pi = new PageInfo(current);
		int count = accountMapper.count();
		pi.setCount(count);
		List<Account> findByid=accountMapper.findByid(pi.getStart(), pi.getOffset());
		System.out.println("总的记录数:"+count+"--"+pi);
		pi.setList(findByid);
		return pi;
	}
	/**
	 * 单个用户的查询
	 * @param current
	 * @param accountruename
	 * @return
	 */
	public PageInfo account1(String accountname,int current) {
		PageInfo pi = new PageInfo(current);
		int count = 1;
		pi.setCount(count);
		List<Account> findByid=accountMapper.findByid1(accountname,pi.getStart(), pi.getOffset());
		System.out.println("总的记录数:"+count+"--"+pi);
		pi.setList(findByid);
		return pi;
	}

	/**
	 * 增加用户
	 */
	@Override
	public int insert(Account account) {
		
		int count=accountMapper.insert(account);
		if(count>0){
			return count;
		}else{
			throw new RuntimeException("增加失败");
		}
	}
	/**
	 * 查询客户
	 */
	@Override
	public List<Account> selectByName(String accountname) {
		List<Account> result=accountMapper.selectByname(accountname);
		return result;
		
	}
	/**
	 * 删除客户
	 */
	@Override
	public int deleteByPrimaryKey(Integer accountid) {
		int result = accountMapper.deleteByPrimaryKey(accountid);
		return result;
	}

	@Override
	public Account selectByPrimaryKey(int accountid) {
		Account account = accountMapper.selectByPrimaryKey(accountid);
		return account;
	}
	/**
	 * 编辑客户
	 */
	@Override
	public int updateByPrimaryKeySelective(Account account) {
		int result=accountMapper.updateByPrimaryKeySelective(account);
		if(result>0){
			return result;
		}else{
			throw new RuntimeException("修改失败");
		}
	}

	/*@Override
	public int checkaccountname(String accountname) {
		int result =accountMapper.checkaccountname(accountname);
		return result;
	}*/
}
